<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>ZDH 通道新增首页</title>
    <meta name="keywords" content="ZDH 通道新增首页">
    <meta name="description" content="ZDH 通道新增首页">

    <link rel="shortcut icon" href="img/favicon.ico">

    <link href="css/plugins/chosen/chosen.css" rel="stylesheet">
    <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">
    <link href="css/plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
    <link href="css/plugins/datetimepicker/datetimepicker.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="css/jsplumb/style.css">

    <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="js/plugins/magicsuggest/magicsuggest.css" rel="stylesheet">
    <style>

    </style>
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">

    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>通道配置</h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="dropdown-toggle" data-toggle="dropdown" href="buttons.html#">
                            <i class="fa fa-eye"></i>
                        </a>
                        <ul class="dropdown-menu dropdown-user">
                            <li><a href="javascript:void(0);" onclick="getResourceDesc()">功能说明</a>
                            </li>
                        </ul>
                    </div>
                </div>
                <div class="ibox-content">

                    <form id="dispatch_task_add_form" name="dispatch_task_add_form" method="post" class="form-horizontal"
                          action="">
                        <input type="hidden" id="pool_config" name="pool_config" value="">

                        <div class="form-group">
                            <label class="col-sm-2 control-label">归属产品</label>
                            <div class="col-sm-10">
                                <select id="product_code" name="product_code"
                                        data-placeholder="归属产品...."
                                        class="chosen-select form-control m-b" >
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">归属组</label>
                            <div class="col-sm-10">
                                <select id="dim_group" name="dim_group"
                                        data-placeholder="归属组...."
                                        class="chosen-select form-control m-b" >
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">通道池名称</label>
                            <div class="col-sm-10">
                                <input id="pool_name" name="pool_name" type="text"
                                       placeholder="通道名池称,不可为空" class="form-control" aria-required="true"> </span>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">通道池码</label>
                            <div class="col-sm-10">
                                <input id="pool_code" name="pool_code" type="text"
                                       placeholder="通道池码,不可为空" class="form-control" aria-required="true"> </span>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">短信通道</label>
                            <div class="col-sm-10">
                                <select id="sms_channel_config" name="sms_channel_config" multiple
                                        data-placeholder="选择通道..."
                                        class="chosen-select form-control m-b" tabindex="2">

                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">邮件通道</label>
                            <div class="col-sm-10">
                                <select id="email_channel_config" name="email_channel_config" multiple
                                        data-placeholder="选择通道..."
                                        class="chosen-select form-control m-b" tabindex="2">

                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">APP通道</label>
                            <div class="col-sm-10">
                                <select id="app_channel_config" name="app_channel_config" multiple
                                        data-placeholder="选择通道..."
                                        class="chosen-select form-control m-b" tabindex="2">

                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">小程序通道</label>
                            <div class="col-sm-10">
                                <select id="miniprogram_channel_config" name="miniprogram_channel_config" multiple
                                        data-placeholder="选择通道..."
                                        class="chosen-select form-control m-b" tabindex="2">

                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">微信公众号</label>
                            <div class="col-sm-10">
                                <select id="wechatofficialaccount_channel_config" name="wechatofficialaccount_channel_config" multiple
                                        data-placeholder="选择通道..."
                                        class="chosen-select form-control m-b" tabindex="2">

                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">飞书机器人通道</label>
                            <div class="col-sm-10">
                                <select id="feishurobot_channel_config" name="feishurobot_channel_config" multiple
                                        data-placeholder="选择通道..."
                                        class="chosen-select form-control m-b" tabindex="2">

                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">飞书通道</label>
                            <div class="col-sm-10">
                                <select id="feishu_channel_config" name="feishu_channel_config" multiple
                                        data-placeholder="选择通道..."
                                        class="chosen-select form-control m-b" tabindex="2">

                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">其他参数</label>
                            <div class="col-sm-10">
                                <textarea id="params" name="params" class="form-control" rows="5" placeholder="其他参数,k1=v1 结构,多个参数换行"></textarea>
                            </div>

                        </div>


                        <div class="hr-line-dashed"></div>


                        <div class="form-group">
                            <div class="text-center">
                                <button id="save_dispatch_task" name="save_zdh" class="btn btn-primary" οnsubmit='return false'
                                        type="button">保存
                                </button>
                                <button id="update_dispatch_task" name="save_zdh" class="btn btn-primary" οnsubmit='return false'
                                        type="button">更新
                                </button>
                                <button id="reset" class="btn btn-white" type=reset>清空</button>
                            </div>
                        </div>

                    </form>


                </div>
            </div>



        </div>
    </div>
</div>


<script src="js/zdh_common.js"></script>
<!-- 全局js -->
<script src="js/jquery.min.js?v=2.1.4"></script>
<script src="js/jquery-ui.custom.min.js?v=2.1.4"></script>
<script src="js/jquery-ui-1.10.4.min.js"></script>
<script src="js/bootstrap.min.js?v=3.3.6"></script>

<!--<script type="text/javascript" src="js/plugins/suggest/bootstrap-suggest.min.js"></script>-->
<script type="text/javascript" src="js/plugins/magicsuggest/magicsuggest.js"></script>

<!-- 自定义js -->
<script src="js/plugins/metisMenu/jquery.metisMenu.js"></script>
<script src="js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<script src="js/hplus.js?v=4.1.0"></script>
<script src="js/content.js?v=1.0.0"></script>

<!-- Chosen -->
<script src="js/plugins/chosen/chosen.jquery.js"></script>

<!-- layer javascript -->
<script src="js/plugins/layer/layer.min.js"></script>

<!-- Toastr script -->
<script src="js/plugins/toastr/toastr.min.js"></script>

<!-- iCheck -->
<script src="js/plugins/iCheck/icheck.min.js"></script>

<script src="js/plugins/datetimepicker/bootstrap-datetimepicker.min.js"></script>

<script src="js/plugins/datetimepicker/bootstrap-datetimepicker.zh-CN.js"></script>

<script src="js/plugins/layer/laydate/laydate.js"></script>

<script type="text/javascript" src="js/jsplumb/jquery.jsPlumb.min.js"></script>

<script src="js/admin/dim_product_common.js"></script>
<script src="js/admin/dim_group_common.js"></script>

<script>
    $(document).ready(function () {
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });

    });

    (function(document, window, $) {


        (function () {

            var url = location.search; //这一条语句获取了包括问号开始到参数的最后，不包括前面的路径
            var params = url.substr(1);//去掉问号
            var pa = params.split("&");
            var s = new Object();
            for(var i = 0; i < pa.length; i ++){
                s[pa[i].split("=")[0]] = unescape(pa[i].split("=")[1]);
            }

            function init_push_channel(id, channel_type){
                $.ajax({
                    type: 'POST',
                    url: server_context+"/push_channel_list",
                    async:false,
                    dataType: 'json',
                    data: {"channel_type": channel_type},
                    //成功返回
                    success: function (data) {
                        if(data.code != "200"){
                            layer.msg(data.msg);
                            return ;
                        }
                        var str = '<option value=\"'  + '\" hassubinfo=\"true\">' + '选择通道' + '</option>';
                        for (var i = 0; i < data.result.length; i++) {
                            str += '<option value=\"' + data.result[i].channel_code + '\" hassubinfo=\"true\">' + data.result[i].channel_name + '</option>';
                        }
                        $('#'+id).html(str);
                        $('#'+id).trigger("chosen:updated");
                        $('#'+id).chosen();
                    },
                    //处理完成
                    complete: function () {
                    },
                    //报错
                    error: function (data) {
                    }
                });
            }
            init_push_channel('sms_channel_config', "sms");
            init_push_channel('email_channel_config', "email");
            init_push_channel('app_channel_config', "app");
            init_push_channel('miniprogram_channel_config', "miniprogram");
            init_push_channel('wechatofficialaccount_channel_config', "wechatofficialaccount");
            init_push_channel('feishurobot_channel_config', "feishurobot");
            init_push_channel('feishu_channel_config', "feishu");
            //init_job_sources();
            //s.id=-1 表示新增,否则表示更新
            if(s.id==-1){
                $('#update_dispatch_task').hide();
                $('#save_dispatch_task').show();
            }else{
                if(s.is_copy == "true"){
                    console.info("拷贝按钮触发");
                    $('#update_dispatch_task').hide();
                    $('#save_dispatch_task').show();
                }else{
                    console.info("更新按钮触发");
                    $('#save_dispatch_task').hide();
                    $('#update_dispatch_task').show();
                }

            }

            if(s.id !='-1'){
                console.info("更新-拷贝按钮触发");
                //开始赋值
                $.ajax({
                    url : server_context+"/push_channel_pool_detail",
                    data : "id=" + s.id,
                    type : "post",
                    dataType : "json",
                    success : function(data) {
                        if(data.code != "200"){
                            layer.msg(data.msg);
                            return ;
                        }
                        console.info("success");
                       // layer.alert(JSON.stringify(data[0]))
                        //获取调度任务说明

                        var product_code=data.result.product_code;
                        $("#product_code").val(product_code);
                        $("#product_code").trigger("chosen:updated");

                        var dim_group=data.result.dim_group;
                        $("#dim_group").val(dim_group);
                        $("#dim_group").trigger("chosen:updated");

                        var pool_name=data.result.pool_name;
                        $("#pool_name").val(pool_name);

                        var pool_code=data.result.pool_code;
                        $("#pool_code").val(pool_code);

                        var pool_config = JSON.parse(data.result.pool_config);

                        $("#sms_channel_config").val(pool_config.sms_config);
                        $("#sms_channel_config").trigger("chosen:updated");

                        $("#email_channel_config").val(pool_config.email_config);
                        $("#email_channel_config").trigger("chosen:updated");

                        $("#wechatofficialaccount_channel_config").val(pool_config.wechatofficialaccount_config);
                        $("#wechatofficialaccount_channel_config").trigger("chosen:updated");

                        $("#feishurobot_channel_config").val(pool_config.feishurobot_config);
                        $("#feishurobot_channel_config").trigger("chosen:updated");

                        $("#feishu_channel_config").val(pool_config.feishu_config);
                        $("#feishu_channel_config").trigger("chosen:updated");

                        $("#params").val(pool_config.params);

                    },
                    error: function (data) {
                        console.info("error: " + data.responseText);
                    }

                });
            }


        })();



    })(document, window, jQuery);


    function buildConfig(){
        var config = {
            "sms_config": $("#sms_channel_config").val(),
            "email_config": $("#email_channel_config").val(),
            "app_config": $("#app_channel_config").val(),
            "miniprogram_config": $("#miniprogram_channel_config").val(),
            "wechatofficialaccount_config": $("#wechatofficialaccount_channel_config").val(),
            "feishurobot_config": $("#feishurobot_channel_config").val(),
            "feishu_config": $("#feishu_channel_config").val(),
            "params": $("#params").val(),
        };

        return config;
    }

    $('#save_dispatch_task').click(function () {

        if($('#pool_name').val()==''){
            layer.msg("通道名称不可为空");
            return false
        }


        var config = buildConfig();
        $("#pool_config").val(JSON.stringify(config));

        $.ajax({
            type: 'POST',
            url: server_context+"/push_channel_pool_add",
            dataType: 'json',
            data: $("#dispatch_task_add_form").serialize(),
            //发送数据前
            beforeSend: function () {
                // 禁用按钮防止重复提交
                $("#save_dispatch_task").attr({disabled: "disabled"});
            },
            //成功返回
            success: function (data) {
                if(data.code != "200"){
                    layer.msg(data.msg);
                    return
                }
                $("#save_dispatch_task").removeAttr('disabled');
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
                //closeTab();
            },
            //处理完成
            complete: function () {
                $("#save_dispatch_task").removeAttr('disabled');
                console.info("complete")
            },
            //报错
            error: function (data) {
                $("#save_dispatch_task").removeAttr('disabled');
                layer.msg(data.responseText);
                console.info("error: " + data.responseText);
            }
        });


    });

    $('#update_dispatch_task').click(function () {

        var url = location.search; //这一条语句获取了包括问号开始到参数的最后，不包括前面的路径
        var params = url.substr(1);//去掉问号
        var pa = params.split("&");
        var s = new Object();
        for(var i = 0; i < pa.length; i ++){
            s[pa[i].split("=")[0]] = unescape(pa[i].split("=")[1]);
        }


        if($('#channel_name').val()==''){
            layer.msg("通道名称不可为空");
            return false
        }

        var config = buildConfig();
        $("#pool_config").val(JSON.stringify(config));

        $.ajax({
            type: 'POST',
            url: server_context+"/push_channel_pool_update",
            dataType: 'json',
            data: $("#dispatch_task_add_form").serialize()+'&id='+s.id,
            //发送数据前
            beforeSend: function () {
                // 禁用按钮防止重复提交
                $("#update_dispatch_task").attr({disabled: "disabled"});
            },
            //成功返回
            success: function (data) {
                if(data.code != '200'){
                    parent.layer.msg("更新失败"+data.msg);
                    return ;
                }
                $("#update_dispatch_task").removeAttr('disabled');
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
               // closeTab()
            },
            //处理完成
            complete: function () {
                $("#update_dispatch_task").removeAttr('disabled');
                console.info("complete")
            },
            //报错
            error: function (data) {
                $("#update_dispatch_task").removeAttr('disabled');
                layer.msg(data.responseText);
                console.info("error: " + data.responseText);
            }
        });

    });

</script>


</body>

</html>
